//
//  LoginViewController.m
//  SoutheastAsia
//
//  Created by rimi on 16/1/11.
//  Copyright © 2016年 zlj. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "LEffectLabel.h"
#import "CustomLoginViewController.h"

@interface LoginViewController ()


@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIImageView *backGroundImageView;
@property (nonatomic, strong) LEffectLabel *registerLabel;
@property (nonatomic, strong) UIImageView *downImageView;
@property (nonatomic, strong) UIButton *backButton;

@property (nonatomic, strong) UISwipeGestureRecognizer *downGesture;
@property (nonatomic, strong) UIView *downView;


- (void)initialApperence;

@end

@implementation LoginViewController
#pragma mark - lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initialApperence];
}
#pragma mark - private method
- (void)initialApperence {
    
    [self.view addSubview:self.backGroundImageView];
    [self.view addSubview:self.loginButton];
    [self.view addSubview:self.backButton];
    [self.view addSubview:self.downView];
    [self.downView addSubview:self.registerLabel];

    [self performSelector:@selector(addAnimation) withObject:nil afterDelay:5];
}

- (void)addAnimation
{

    for (int i = 0; i < 1000; i++)
    {
        int64_t delayInSeconds = 2 * i;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            self.registerLabel.effectDirection = EffectDirectionTopToBottom;
            [self.registerLabel performEffectAnimation];
        });
    }
}
#pragma mark - interface method
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

#pragma mark - callback / action
//登陆按钮点击事件
- (void)isLoginTaped:(UIButton *)sender {
    CustomLoginViewController *customVC = [[CustomLoginViewController alloc] init];
    customVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//    [self presentViewController:customVC animated:YES completion:nil];
    [self.navigationController pushViewController:customVC animated:YES];
    
}
//返回按钮点击事件
- (void)isBackTaped:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)backGestureTaped:(UISwipeGestureRecognizer *)gesture {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)registerGestureTaped:(UISwipeGestureRecognizer *)gesture {
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    registerVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:registerVC animated:YES completion:nil];
}
#pragma mark - getter

//登陆按钮
- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginButton.frame = CGRectMake(0, 0, 140, 80);
        _loginButton.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));
        [_loginButton setTitle:@"Sign in" forState:UIControlStateNormal];
        [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
        [_loginButton.titleLabel setFont:[UIFont fontWithName:@"Georgia" size:30]];
        [_loginButton.titleLabel setFont:[UIFont systemFontOfSize:30 weight:0.5]];
        [_loginButton addTarget:self action:@selector(isLoginTaped:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginButton;
}

//背景
- (UIImageView *)backGroundImageView {
    if (!_backGroundImageView) {
        _backGroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LoginBackground1.png"]];
        _backGroundImageView.frame = self.view.frame;
    }
    return _backGroundImageView;
}
//注册标签
- (LEffectLabel *)registerLabel {
    if (!_registerLabel) {
        _registerLabel = ({
            
            LEffectLabel *label = [[LEffectLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 120)];
                          label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
            
            [label setTextColor:[UIColor whiteColor]];
            label.effectColor = [UIColor blackColor];
            [label setBackgroundColor:[UIColor redColor]];
            [label setText:@"Register"];
            [label setFont:[UIFont fontWithName:@"Georgia" size:30]];
            label.userInteractionEnabled = YES;
            label.center = CGPointMake(100, 50);

            label;
        });
    }
    return _registerLabel;
}


//注册手势
- (UISwipeGestureRecognizer *)downGesture {
    if (!_downGesture) {
        _downGesture = ({
            UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(registerGestureTaped:)];
            gesture.direction = UISwipeGestureRecognizerDirectionDown;
            gesture;
        });
    }
    return _downGesture;
}
- (UIView *)downView {
    if (!_downView) {
        _downView = ({
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
            view.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMaxY(self.view.frame) - 60);
            view.backgroundColor = [UIColor clearColor];
            [view addSubview:self.registerLabel];
            [view addSubview:self.downImageView];
            [view addGestureRecognizer:self.downGesture];
            
            view;
        });
    }
    return _downView;
}
- (UIImageView *)downImageView {
    if (!_downImageView) {
        _downImageView = ({
            UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"箭头.png"]];
            view.bounds = CGRectMake(0, 0, 64, 40);
            view.center = CGPointMake(100, 80);
            view;
        });
    }
    return _downImageView;
}
- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
            button.layer.borderColor = [UIColor blackColor].CGColor;
            button.frame = CGRectMake(30, 30, 50, 50);
            [button addTarget:self action:@selector(isBackTaped:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _backButton;
}
@end
